
<template>
  <div class="xtx-cart-page">
    <div class="container">

      <div class="cart">
        <table>
          <thead>
            <tr>
              <th>
                <div class="xtx-checkbox" @click="changeAll()">
                  <!-- 选中小图标 -->
                  <i v-if="checked" class="iconfont icon-xuanzhong"></i>
                  <!-- 未选中小图标 -->
                  <i v-else class="iconfont icon-danxuanweixuanzhong"></i>
                  全选
                </div>
              </th>
              <th>商品信息</th>
              <th>单价</th>
              <th>数量</th>
              <th>小计</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="(good, index) in validList" :key="good.id">
              <td>
                <div class="xtx-checkbox" @click="changeChecked(index)">
                  <i v-if="good.checked" class="iconfont icon-xuanzhong"></i>
                  <i v-else class="iconfont icon-danxuanweixuanzhong"></i>
                </div>
              </td>
              <td>
                <div>
                  <p>
                    {{ good.title }}
                  </p>
                </div>
              </td>
              <td>
                <p>&yen;{{ good.nowPrice }}</p>
              </td>
              <td>
                <p class="inline"><button @click="add(index)"> + </button></p>
                <p class="inline">{{ good.count }}</p>
                <p class="inline"><button @click="reduce(index)"> - </button></p>
              </td>
              <td>
                <!-- 当前行总价 -->
                <p>
                  &yen;{{ (good.nowPrice * good.count).toFixed(2) }}
                </p>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div>
        <span>
          总计为 &yen;{{ TotalPrice }}
        </span>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'App',
  data() {
    return {
      validList: [
        {
          id: 1,
          title: "玻璃凉水壶",
          nowPrice: '90',
          count: 5,
          checked: false
        },
        {
          id: 2,
          title: "鞋子",
          nowPrice: '100',
          count: 3,
          checked: false
        },
        {
          id: 3,
          title: "凉水壶",
          nowPrice: '20',
          count: 3,
          checked: false
        }
      ],
      TotalPrice: 0,
      checked: false
    }
  },
  methods: {
    changeChecked(index) {
      this.validList[index].checked = !this.validList[index].checked
      this.getprice(index);
      this.checked = this.validList.every((item) => {
        return item.checked
      })
    },
    add(index) {
      this.validList[index].count += 1;
      if (this.validList[index].checked) {
        this.TotalPrice += this.validList[index].nowPrice * 1
      }
    },
    reduce(index) {
      if (this.validList[index].count != 1) {
        this.validList[index].count -= 1;
        console.log("数量为:", this.validList[index].count);
        if (this.validList[index].checked) {
          this.TotalPrice -= this.validList[index].nowPrice * 1
        }
      } else {
        this.validList[index].count = 1
      }

    },
    getprice(index) {
      if (this.validList[index].checked) {
        this.TotalPrice += this.validList[index].nowPrice * this.validList[index].count * 1
      } else {
        this.TotalPrice -= this.validList[index].nowPrice * this.validList[index].count * 1
      }
    },
    // 全选
    changeAll() {
      this.checked = !this.checked;
      if (this.checked) {
        this.TotalPrice = 0;
        this.validList.forEach(item => {
          item.checked = true;
          this.TotalPrice += item.nowPrice * item.count * 1
        });
      } else {
        this.validList.forEach(item => {
          item.checked = false;
        })
        this.TotalPrice = 0;
      }
    }
  },
}
</script>
<style>
.inline {
  display: inline-block;
}
</style>
